package view.xml
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	import model.ModelLibrary;
	import model.ModelSelection;
	import model.ModelRooms
	import model.ModelSelection;
	import mx.collections.XMLListCollection;
	import mx.events.FlexEvent;
	import view.xml.ScreenXML;
	import org.robotlegs.mvcs.Mediator;
	
	/**
	 * 
	 * @author Marius Dorin Moraru
	 */
	public class MediatorScreenXML extends Mediator {
		[Inject]
		public var v:ScreenXML;
		
		[Inject]
		public var mr:ModelRooms;
		
		[Inject]
		public var ms:ModelSelection;
		
		[Inject]
		public var ml:ModelLibrary;
		
		override public function onRegister() : void  {
			eventMap.mapListener( v, FlexEvent.SHOW, showEventHandler);
			eventMap.mapListener( v.btnExpand, MouseEvent.CLICK, btnExpandClickHandler);
			eventMap.mapListener( v.btnCollapse, MouseEvent.CLICK, btnCollapseClickHandler);
		}
		
		private function btnExpandClickHandler(e:Event = null) : void {
			//v.tree.expandItem(v.tree.dataProvider, true);
			v.tree.openItems = [];
		}
		
		private function btnCollapseClickHandler(e:Event = null) : void {
			//v.tree.expandItem(v.tree.dataProvider, false);
			v.tree.openItems = [];
			v.tree.openItems = (v.tree.dataProvider as XMLListCollection).source;
		}
		
		private function showEventHandler(e:Event = null) : void {
			var xml:XML = <Data/>;
			xml.appendChild(ml.toXML());
			xml.appendChild(mr.toXML());
			
			v.textArea.text = xml.toXMLString();
			v.tree.dataProvider = xml;
			v.tree.labelFunction = labelFunction;
		}
		private function labelFunction(xml:XML) : String {
			var s:String;
			s = xml.name();
			for each(var attr:XML in xml.attributes()) {
				var localName:String = attr.localName()+"";
				var value:String = attr + "";
				s = s + " " + localName + "=" + value;
			}
			return s;
		}
	}
	
}